package cn.edu.nchu.fragmentdemo.myComponents.NewCircleComponents;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;

import androidx.annotation.RequiresApi;

import cn.edu.nchu.fragmentdemo.R;
import cn.edu.nchu.fragmentdemo.MainActivity;
import cn.edu.nchu.fragmentdemo.myComponents.components.MyMarkerClass;
import cn.edu.nchu.fragmentdemo.myComponents.overLays.RouteLay;
import cn.edu.nchu.fragmentdemo.utils.MarkDrawable;
import cn.edu.nchu.fragmentdemo.utils.CommenUtils;
import cn.edu.nchu.fragmentdemo.utils.ImageUtil;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.Projection;

import org.osmdroid.views.overlay.Overlay;

//import org.none.advancetactics.fragments.manager.MyFragmentManager;
//import org.none.advancetactics.utils.AppDataManager;


import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @author Tsuki
 * @className: UavRouteTaskFragment
 * @description:航点环形菜单
 **/
public class NewCirclePopupMenu_WayPoint {
    private String TAG = "NewCirclePopupMenu_WayPoint";
    private PopupWindow popupMenu;
    private Context context;
    private MapView mMapView;
    private int menuType=1;
    private View menuView;
    private Point point;
    private GeoPoint geoPoint;
    private MyMarkerClass marker;
    private RouteLay overlay;
    public RingMenuWidget ringMenuWidget0,ringMenuWidget1,ringMenuWidget2,ringMenuWidget3;
    public RingMenuItem mMenu01, mMenu02, mMenu03, mMenu04, mMenu05, mMenu06, mMenu07;
    public RingMenuItem mMenu11, mMenu12, mMenu13, mMenu14, mMenu15, mMenu16, mMenu17;
    private MainActivity mainActivity;
    CommenUtils commenUtils = new CommenUtils();
    String uid;

    public NewCirclePopupMenu_WayPoint(Context context, MyMarkerClass marker, Overlay overlay){
        mainActivity = (MainActivity)MainActivity.getActivity();
        mMapView = mainActivity.getMap();
        this.context = mainActivity.getApplicationContext();
        Projection projection = mMapView.getProjection();
        geoPoint = new GeoPoint(marker.getPosition().getLatitude(),marker.getPosition().getLongitude());
        this.point = (Point)projection.toPixels(geoPoint,this.point);
        this.marker = marker;
        this.overlay = (RouteLay)overlay;
    }

    /**
     * 对外调用
     * 显示不同对应类型的菜单
     */
    public void showPopupMenu(){
        /*switch (marker.getPopMenuType()){
            case "type0":{
                markCircleMenu0();
                menuView = ringMenuWidget0;
                break;
            }
        }*/
        markCircleMenu0();
        menuView = ringMenuWidget0;
        popupMenu = new PopupWindow(menuView, 500,500,true);
        popupMenu.setOutsideTouchable(true);
        popupMenu.setFocusable(true);
        popupMenu.setTouchable(true);
        int height = mMapView.getMeasuredHeight();
        //popupMenu.showAsDropDown(mMapView,point.x-255,point.y-height-295);
        //popupMenu.showAsDropDown(mMapView,point.x-378,point.y-height-395);
//        if(AppDataManager.getInstance().realMapWidth < 2560){//大概是0.704,250*0.704,500-250*0.704
//            popupMenu.showAsDropDown(mMapView,point.x-176,point.y+324);
//        }
//        else{
            popupMenu.showAsDropDown(mMapView,point.x-250,point.y+250);
//        }
    }

    public void markCircleMenu0(){
        ringMenuWidget0 = new RingMenuWidget(context,300,300);
        ringMenuWidget0.setCenterLocation(100,100);
        mMenu01 = new RingMenuItem("CP");//圆环按钮对象实例化
        mMenu01.setOnMenuItemPressed(new RingMenuItem.RingMenuItemClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void execute() throws InterruptedException {//设置点击该按钮的事件
                toCp();
                dismiss();
            }
        });
        mMenu02 = new RingMenuItem("IP");
        mMenu02.setOnMenuItemPressed(new RingMenuItem.RingMenuItemClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void execute() {
                //toIp();
                dismiss();
            }
        });
        mMenu03 = new RingMenuItem("EP");
        mMenu03.setOnMenuItemPressed(new RingMenuItem.RingMenuItemClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void execute() {
                toEp();
                dismiss();
            }
        });
        mMenu04 = new RingMenuItem("加");
        mMenu04.setOnMenuItemPressed(new RingMenuItem.RingMenuItemClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void execute() {
                add();
                dismiss();
            }
        });
        mMenu05 = new RingMenuItem("");
        mMenu05.setDisplayIcon(R.drawable.cancel);
        mMenu05.setOnMenuItemPressed(new RingMenuItem.RingMenuItemClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void execute() {
                dismiss();
            }
        });
        mMenu06 = new RingMenuItem("");
        mMenu06.setDisplayIcon(R.drawable.delete);
        mMenu06.setOnMenuItemPressed(new RingMenuItem.RingMenuItemClickListener() {
            @Override
            public void execute()  {
                alertDialog();
                dismiss();
            }
        });
        mMenu07 = new RingMenuItem("恢复");
        mMenu07.setOnMenuItemPressed(new RingMenuItem.RingMenuItemClickListener() {
            @Override
            public void execute() {
                toResume();
                dismiss();
            }
        });
        ringMenuWidget0.setCenterIconSize(50, 80);//设置最小尺寸和最大尺寸
        ringMenuWidget0.setRingRadius(40, 90);//设置内环半径64 外环半径116
        ringMenuWidget0.setBorderWidth(2);//设置分割线宽度为8
        ringMenuWidget0.setTextSize(22);//设置文本字体大小
        ringMenuWidget0.setBorderColor(Color.TRANSPARENT, 0);//设置分割线颜色为白色
        //ringMenuWidget1.setDefaultColor(Color.parseColor("#AAAAAA"), 255);//设置圆环背景颜色为灰色

        //swx new 20210423 洛客UI优化 界面整合
        ringMenuWidget0.setDefaultColor(Color.parseColor("#000000"), 178);//设置圆环背景颜色为一般白色
        ringMenuWidget0.setSelectedColor(Color.parseColor("#DD0000"), 255);//设置点击颜色为红色
        //ringMenuWidget0.setDefaultColor(Color.parseColor("#ffffff"), 255);//设置圆环背景颜色为一般白色
        //ringMenuWidget0.setSelectedColor(Color.parseColor("#DD0000"), 255);//设置点击颜色为红色
        ringMenuWidget0.setTextColor(Color.WHITE, 255);//设置文本颜色为白色
        //ringMenuWidget0.setTextColor(Color.BLACK, 255);//设置文本颜色为黑色

        ringMenuWidget0.addMenuItems(new ArrayList<RingMenuItem>() {
            {
                add(mMenu01);
                add(mMenu02);
                add(mMenu03);
                add(mMenu05);
                add(mMenu04);
                add(mMenu06);
                add(mMenu07);
            }
        });
    }

    /**
     * menu消失
     */
    public void dismiss(){
        popupMenu.dismiss();
        popupMenu = null;
    }

   /**
     * 添加信息
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    private void addInfo(){
        CommenUtils commenUtils = new CommenUtils(context);
//        MarkerInfoFragment markerInfoFragment = new MarkerInfoFragment();
//        /**
//         * 向fragment传递对象
//         * marker实体
//         */
//        Bundle bundle = new Bundle();
//        bundle.putSerializable("marker",marker);
//        markerInfoFragment.setArguments(bundle);
//        MyFragmentManager.getInstance().setRightFragment(markerInfoFragment, AppDataManager.getInstance().get800width());
    }

    /**
     * 转化为Ip点
     */
    private void toIp(){
        marker.setId("70");
        Bitmap mBitmap = BitmapFactory.decodeResource(context.getResources(), commenUtils.getResource("ip_18"));
        mBitmap = ImageUtil.drawTextToCenter(context,mBitmap,"70",10, Color.BLACK);
        MarkDrawable drawable = new MarkDrawable(context.getResources(), mBitmap);
        marker.setIcon(drawable);
        //回调刷新左侧listview中的内容
        try {
            overlay.setIPTitle(marker);
            overlay.sendMessage("change");
            mMapView.invalidate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 转化为cp点
     */
    private void toCp(){
        marker.setId("60");
        Bitmap mBitmap = BitmapFactory.decodeResource(context.getResources(), commenUtils.getResource("cp_18"));
        mBitmap = ImageUtil.drawTextToCenter(context,mBitmap,"60",10, Color.BLACK);
        MarkDrawable drawable = new MarkDrawable(context.getResources(), mBitmap);
        marker.setIcon(drawable);
        //回调刷新左侧listview中的内容
        try {
            overlay.setCPTitle(marker);
            overlay.sendMessage("change");
            mMapView.invalidate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 转化为ep点
     */
    private void toEp(){
        marker.setId("78");
        Bitmap mBitmap = BitmapFactory.decodeResource(context.getResources(), commenUtils.getResource("ep_18"));
        mBitmap = ImageUtil.drawTextToCenter(context,mBitmap,"78",10, Color.BLACK);
        MarkDrawable drawable = new MarkDrawable(context.getResources(), mBitmap);
        marker.setIcon(drawable);
        //回调刷新左侧listview中的内容
        try {
            overlay.setEPArrawPos();
            overlay.setEPTitle(marker);
            overlay.sendMessage("change78");
            mMapView.invalidate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //地图界面加入退出方向表示

    }
    /**
     * 转化为无效点
     * 暂无，是指整个航路的航点有效性
     */
    private void toInvalid(){
        marker.setId("0");
        Bitmap mBitmap = BitmapFactory.decodeResource(context.getResources(), commenUtils.getResource("routpoint_18"));
        mBitmap = ImageUtil.drawTextToCenter(context,mBitmap,"0",12, Color.RED);
        MarkDrawable drawable = new MarkDrawable(context.getResources(), mBitmap);
        marker.setIcon(drawable);
        //回调刷新左侧listview中的内容
        try {
            overlay.sendMessage("change");
            mMapView.invalidate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 恢复原来点
     */
    private void toResume(){
        int i = Integer.parseInt(marker.getSubDescription().toString());
        if(marker.getId().equals("78")){
            try {
                overlay.hideEPTitle();
                overlay.sendMessage("resume78");
                mMapView.invalidate();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(marker.getId().equals("70")){
            overlay.hideIPTitle();
        }
        if(marker.getId().equals("60")){
            overlay.hideCPTitle();
        }
        marker.setId(i+60+"");
        Bitmap mBitmap = BitmapFactory.decodeResource(context.getResources(), commenUtils.getResource("routpoint_18"));
        mBitmap = ImageUtil.drawTextToCenter(context,mBitmap,i+60+"",10, Color.BLACK);
        MarkDrawable drawable = new MarkDrawable(context.getResources(), mBitmap);
        marker.setIcon(drawable);
        //回调刷新左侧listview中的内容
        try {
            overlay.sendMessage("change");
            mMapView.invalidate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加一个航点
     */
    private void add(){
        try {
            if(marker.getId().equals("60")){
//                mainActivity.showToast("该航点是第一个点，无法添加新的航点！");
                return;
            }
//            overlay.addPoint(marker);
            overlay.sendMessage("change");
            mMapView.invalidate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除一个航点
     */
    private void delete(){
        try {
            overlay.deletePoint(marker);
            overlay.sendMessage("change");
            mMapView.invalidate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void alertDialog() {
        final AlertDialog dialog = new AlertDialog.Builder(MainActivity.getActivity()).create();
        dialog.show();  //注意：必须在window.setContentView之前show
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setContentView(R.layout.activity_dialog);
        Button exitBtn0= (Button) dialogWindow.findViewById(R.id.exitBtn0);
        Button exitBtn1= (Button) dialogWindow.findViewById(R.id.exitBtn1);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.25); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
        //点击确定按钮让对话框消失
        exitBtn0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delete();
                dialog.dismiss();
            }
        });
        //点击确定按钮让对话框消失
        exitBtn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }



}